package com.ewhale.feitengguest.ui.auth

import android.graphics.Color
import android.os.Bundle
import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.core.content.ContextCompat
import butterknife.BindView
import butterknife.OnClick
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.presenter.user.auth.ForgetPasswordPresenter
import com.ewhale.feitengguest.view.auth.ForgetPasswordView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.CheckUtil
import com.simga.library.utils.HawkKey
import com.simga.library.utils.StringUtil
import com.simga.library.widget.BGButton
import kotlinx.android.synthetic.main.activity_forget_password.*

class ForgetPasswordActivity : MBaseActivity<ForgetPasswordPresenter?>(), ForgetPasswordView, TextWatcher {


    private var isPwdShow = false
    private var isPwd2Show = false
    private var currentTab = 0
    override fun onGetBundle(bundle: Bundle) {
        currentTab = bundle.getInt("currentTab")
    }

    override fun getViewId(): Int {
        return R.layout.activity_forget_password
    }

    override fun init(savedInstanceState: Bundle?) {
        setTitle("忘记密码")
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    override fun initListener() {
        et_phone!!.addTextChangedListener(this)
        et_registe_password!!.addTextChangedListener(this)
        et_registe_password_again!!.addTextChangedListener(this)
        et_code!!.addTextChangedListener(this)
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
    }

    @OnClick(R.id.btn_getcode, R.id.iv_eye1, R.id.iv_eye2, R.id.btn_commit)
    fun onViewClicked(view: View) {
        val registerPhone: String
        val code: String
        val registerPwd: String
        val registerPwd2: String
        when (view.id) {
            R.id.btn_getcode -> {
                registerPhone = et_phone!!.text.toString()
                when {
                    CheckUtil.isNull(registerPhone) -> showToast("请输入手机号码")
                    !StringUtil.isMobile(registerPhone) -> showToast("请输入正确手机号码")
                    else -> presenter!!.getCode(if (currentTab == 0) Hawk.get(HawkKey.USER_AUTHENTICATION, "") else Hawk.get(HawkKey.SHOP_AUTHENTICATION, ""), registerPhone)
                }
            }
            R.id.iv_eye1 -> {
                et_registe_password!!.transformationMethod = if (isPwdShow) PasswordTransformationMethod.getInstance() else HideReturnsTransformationMethod.getInstance()
                isPwdShow = !isPwdShow
            }
            R.id.iv_eye2 -> {
                et_registe_password_again!!.transformationMethod = if (isPwd2Show) PasswordTransformationMethod.getInstance() else HideReturnsTransformationMethod.getInstance()
                isPwd2Show = !isPwd2Show
            }
            R.id.btn_commit -> {
                registerPhone = et_phone!!.text.toString()
                code = et_code!!.text.toString()
                registerPwd = et_registe_password!!.text.toString()
                registerPwd2 = et_registe_password_again!!.text.toString()
                when {
                    CheckUtil.isNull(registerPhone) -> showToast("请输入手机号码")
                    !StringUtil.isMobile(registerPhone) -> showToast("请输入正确手机号码")
                    !CheckUtil.checkEqual(registerPwd, registerPwd2) -> showToast("两次密码不同，请重新确认密码")
                    else -> presenter!!.resetPwd(currentTab, registerPhone, code, registerPwd)
                }
            }
        }
    }

    /**
     * 检查输入框
     *
     * @return
     */
    fun checkInput(): Boolean {
        return !(CheckUtil.isNull(et_phone!!.text.toString()) ||
                CheckUtil.isNull(et_code!!.text.toString()) ||
                CheckUtil.isNull(et_registe_password!!.text.toString()) ||
                CheckUtil.isNull(et_registe_password_again!!.text.toString()))
    }

    override fun getCodeSuccess() {
        showToast("验证码已发送，请注意查收")
        mTimer.start()
    }

    private val mTimer: CountDownTimer = object : CountDownTimer(60 * 1000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            btn_getcode!!.run {
                text = (millisUntilFinished / 1000).toString() + "s"
                setTextColor(ContextCompat.getColor(mContext, R.color.text_666666))
                isEnabled = false
                setNormalSolid(ContextCompat.getColor(mContext, R.color.default_window_color))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.default_window_color))
            }
        }

        override fun onFinish() {
            btn_getcode!!.run {
                text = "重新获取"
                setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
                isEnabled = true
                setNormalSolid(ContextCompat.getColor(mContext, R.color.white))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color))
            }
        }
    }

    override fun resetSuccess() {
        showToast("密码重置成功，请使用新密码登录")
        finish()
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        if (!checkInput()) {
            btn_commit!!.run {
                setNormalSolid(Color.parseColor("#EEEEEE"))
                setNormalStroke(1, Color.parseColor("#D4D4D4"))
                setTextColor(Color.parseColor("#BBBBC2"))
            }
        } else {
            btn_commit!!.run {
                setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color))
                setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color))
                setTextColor(Color.parseColor("#ffffff"))
            }
        }
    }

    override fun afterTextChanged(s: Editable) {}

    companion object {
        @JvmStatic
        fun open(mContext: MBaseActivity<*>, currentTab: Int) {
            val bundle = Bundle()
            bundle.putInt("currentTab", currentTab)
            mContext.startActivity(bundle, ForgetPasswordActivity::class.java)
        }
    }
}